// tests/test-all.js
import { exec } from 'child_process';
import { promisify } from 'util';
const execPromise = promisify(exec);

async function runTest(name, command) {
  console.log(`\n=== ${name} ===`);
  try {
    const { stdout, stderr } = await execPromise(command);
    console.log(stdout);
    if (stderr) {
      console.error('STDERR:', stderr);
    }
    console.log(`${name} completed successfully!`);
    return true;
  } catch (error) {
    console.error(`${name} failed:`);
    console.error(error.message);
    if (error.stdout) console.log('STDOUT:', error.stdout);
    if (error.stderr) console.error('STDERR:', error.stderr);
    return false;
  }
}

async function runAllTests() {
  console.log('Running all tests...\n');

  // Test 1: Auth tests
  const authTest = await runTest('Auth Tests', 'node tests/test-auth.js');

  // Test 2: API tests
  const apiTest = await runTest('API Tests', 'node tests/test-api.js');

  // Test 3: Task group tests
  const taskGroupTest = await runTest('Task Group Tests', 'node tests/test-task-group.js');

  // Test 4: Task tests
  const taskTest = await runTest('Task Tests', 'node tests/test-task.js');

  console.log('\n=== TEST SUMMARY ===');
  console.log('Auth Tests:', authTest ? 'PASS' : 'FAIL');
  console.log('API Tests:', apiTest ? 'PASS' : 'FAIL');
  console.log('Task Group Tests:', taskGroupTest ? 'PASS' : 'FAIL');
  console.log('Task Tests:', taskTest ? 'PASS' : 'FAIL');

  const allPassed = authTest && apiTest && taskGroupTest && taskTest;
  console.log('\nOverall Result:', allPassed ? 'ALL TESTS PASSED' : 'SOME TESTS FAILED');
  
  process.exit(allPassed ? 0 : 1);
}

runAllTests();